/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package marsaMaroc.controllers;

import java.util.List;
import marsaMaroc.dao.ClientDAO;
import marsaMaroc.model.Client;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author Administrateur
 */
@Controller
public class ClientController {
    
    ClientDAO clientDAO = new ClientDAO();
    
    @RequestMapping(value = "/addClient.htm", method = RequestMethod.POST)
    protected String add(
            @RequestParam("fax") String fax,
            @RequestParam("nompdg") String nom,
            @RequestParam("activite") String activite,
            @RequestParam("tel") String tel
    ) throws Exception {
        
        Client client = new Client();
        client.setFax(fax);
        client.setNomPDG(nom);
        client.setSecteurActivite(activite);
        client.setTelProfessionnel(tel);
        
        clientDAO.add(client);        
        return "redirect:/listClient.htm";
    }
    
     @RequestMapping(value = "/newClient.htm", method = RequestMethod.GET)
    protected ModelAndView newC( ) throws Exception {
        
        return new ModelAndView("client_crud/add");
    }

    
    @RequestMapping(value = "/deleteClient.htm", method = RequestMethod.GET)
    protected ModelAndView delete(
            @RequestParam("id") int id
    ) throws Exception {
        
        Client c = (Client) clientDAO.get(id);        
        clientDAO.delete(c);
        return new ModelAndView("client_crud/list_cli");
    }
    
    
    @RequestMapping(value = "/listClient.htm", method = RequestMethod.GET)
    protected ModelAndView list() throws Exception {
        
       List<Client> list =  clientDAO.getAll();
        System.out.println(list);
        return new ModelAndView("client_crud/list_cli","list",list);
    }
    
      
    @RequestMapping(value = "/showClient.htm", method = RequestMethod.GET)
    protected ModelAndView show(@RequestParam("id") int id) throws Exception {
        
       Client c =  clientDAO.get(id);
        return new ModelAndView("client_side/show_client","c",c);
    }
    
}
